using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LevelSelect : MonoBehaviour
{
    public bool IsSelect = false;
    public Sprite LevelBg;
    private Image image;
    public GameObject[] stars;

    private void Awake()
    {
        image = GetComponent<Image>();
    }
    void Start()
    {
        if( transform.parent.GetChild(0).name == transform.name)
        {
            IsSelect = true;
        }
        else
        {
            int beforeNum = int.Parse(gameObject.name) - 1;
            if(PlayerPrefs.GetInt("level"+beforeNum.ToString()) > 0)
            {
                IsSelect=true;
            }
        }
        if (IsSelect)
        {
            image.overrideSprite = LevelBg;
            transform.Find("num").gameObject.SetActive(true);
            int count = PlayerPrefs.GetInt("level" + gameObject.name);
            if(count > 0)
            {
                for(int i = 0; i < count; i++)
                {
                    stars[i].SetActive(true);
                }
            }
        }
    }

    public void Selected()
    {
        if (IsSelect)
        {
            PlayerPrefs.SetString("nowLevel", "level"+gameObject.name);
            SceneManager.LoadScene(1);
        }
    }
}
